const {ipcRenderer} = require('electron');
const Timer = require('timer.js')

function startWork(){
  const workTimer = new Timer({
    ontick: (ms) => {
      updateTime(ms);
    },
    onend: () => {
      notification();
      //倒数时间到了就会 ipcRenderer.invoke, 向主进程发起通信
      //主进程会创建一个notification, 并根据用户对notification的操作的不同, 视情况对渲染进程发起的 ipcRenderer.invoke 进行返回
    }
  });

  workTimer.start(6);
}

function updateTime(ms){
  const timerContainer = document.getElementById('timer-container');
  const s = (ms / 1000).toFixed(0);
  const ss = s % 60;
  const mm = (s / 60).toFixed(0);
  timerContainer.innerText = `${mm.toString().padStart(2,0)}:${ss.toString().padStart(2,0)}`;
}

async function notification(){
  const res/*← 这里就是进程通信拿到返回结果了*/ = await ipcRenderer.invoke('work-notification');

  if(res === 'rest'){
    alert('休息');
    startWork();

  }else if(res === 'work'){
    startWork();
  }
}

startWork();
